<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 4.2.0">
  <link rel="apple-touch-icon" sizes="180x180" href="/blog/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/blog/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/blog/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/blog/images/logo.svg" color="#222">

<link rel="stylesheet" href="/blog/css/main.css">

<link rel="stylesheet" href="//fonts.googleapis.com/css?family=Lato:300,300italic,400,400italic,700,700italic|LiSu:300,300italic,400,400italic,700,700italic|youyuan:300,300italic,400,400italic,700,700italic&display=swap&subset=latin,latin-ext">
<link rel="stylesheet" href="/blog/lib/font-awesome/css/font-awesome.min.css">
  <link rel="stylesheet" href="/blog/lib/pace/pace-theme-bounce.min.css">
  <script src="/blog/lib/pace/pace.min.js"></script>

<script id="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"zchao666.gitee.io","root":"/blog/","scheme":"Gemini","version":"7.7.2","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12,"onmobile":false},"copycode":{"enable":false,"show_result":false,"style":null},"back2top":{"enable":true,"sidebar":false,"scrollpercent":false},"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"algolia":{"hits":{"per_page":10},"labels":{"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}},"localsearch":{"enable":true,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},"path":"search.xml"};
  </script>

  <meta name="description" content="数据验证数据验证主要用于过滤用户输入的错误数据，保证数据的合法性。数据验证分为客户端验证 和 服务器端验证。客户端验证主要通过JavaScript代码实现数据合法性校验；服务器端是整个应用程序阻止非法数据的最后防线。在SpringMVC中，进行数据验证可以利用Spring自带的验证器框架验证数据，也可以使用JSR303实现数据验证。">
<meta property="og:type" content="article">
<meta property="og:title" content="SpringMVC的数据验证">
<meta property="og:url" content="https://zchao666.gitee.io/blog/2020/03/23/SpringMVC%E7%9A%84%E6%95%B0%E6%8D%AE%E9%AA%8C%E8%AF%81/index.html">
<meta property="og:site_name" content="小郭的博客">
<meta property="og:description" content="数据验证数据验证主要用于过滤用户输入的错误数据，保证数据的合法性。数据验证分为客户端验证 和 服务器端验证。客户端验证主要通过JavaScript代码实现数据合法性校验；服务器端是整个应用程序阻止非法数据的最后防线。在SpringMVC中，进行数据验证可以利用Spring自带的验证器框架验证数据，也可以使用JSR303实现数据验证。">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://img-blog.csdnimg.cn/20200323014337648.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQxMTkzMTMz,size_16,color_FFFFFF,t_70">
<meta property="og:image" content="https://img-blog.csdnimg.cn/20200323014447164.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQxMTkzMTMz,size_16,color_FFFFFF,t_70">
<meta property="article:published_time" content="2020-03-22T17:58:44.000Z">
<meta property="article:modified_time" content="2020-03-22T17:51:27.752Z">
<meta property="article:author" content="小郭">
<meta property="article:tag" content="Java">
<meta property="article:tag" content="数据验证">
<meta property="article:tag" content="JSR303">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://img-blog.csdnimg.cn/20200323014337648.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQxMTkzMTMz,size_16,color_FFFFFF,t_70">

<link rel="canonical" href="https://zchao666.gitee.io/blog/2020/03/23/SpringMVC%E7%9A%84%E6%95%B0%E6%8D%AE%E9%AA%8C%E8%AF%81/">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true
  };
</script>

  <title>SpringMVC的数据验证 | 小郭的博客</title>
  






  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

  
  
		<script src="https://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script>
		<script>
		/* 社会主体核心价值观效果 */
		var a_idx = 0;
		jQuery(document).ready(function ($) {
		  $("body").click(function (e) {
			var a = new Array("富强", "民主", "文明", "和谐", "自由", "平等", "公正", "法治", "爱国", "敬业", "诚信", "友善");
			var $i = $("<span/>").text(a[a_idx]);
			a_idx = (a_idx + 1) % a.length;
			var x = e.pageX,
			  y = e.pageY;
			$i.css({
			  "z-index": 100000000,
			  "top": y - 20,
			  "left": x,
			  "position": "absolute",
			  "font-family": "LiSu",
			  "font-weight": "bold",
			  "color": "#ff6651"
			});
			$("body").append($i);
			$i.animate({
			  "top": y - 180,
			  "opacity": 0
			}, 1500, function () {
			  $i.remove();
			});
		  });
		});
	</script>
  

</head>

<body itemscope itemtype="http://schema.org/WebPage">
  <div class="container use-motion">
    <div class="headband"></div>

    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏">
      <span class="toggle-line toggle-line-first"></span>
      <span class="toggle-line toggle-line-middle"></span>
      <span class="toggle-line toggle-line-last"></span>
    </div>
  </div>

  <div class="site-meta">

    <div>
      <a href="/blog/" class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">小郭的博客</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>


<nav class="site-nav">
  
  <ul id="menu" class="menu">
        <li class="menu-item menu-item-home">

    <a href="/blog/" rel="section"><i class="fa fa-fw fa-home"></i>首页</a>

  </li>
        <li class="menu-item menu-item-tags">

    <a href="/blog/tags/" rel="section"><i class="fa fa-fw fa-tags"></i>标签</a>

  </li>
        <li class="menu-item menu-item-categories">

    <a href="/blog/categories/" rel="section"><i class="fa fa-fw fa-th"></i>分类</a>

  </li>
        <li class="menu-item menu-item-archives">

    <a href="/blog/archives/" rel="section"><i class="fa fa-fw fa-archive"></i>归档</a>

  </li>
        <li class="menu-item menu-item-about">

    <a href="/blog/about/" rel="section"><i class="fa fa-fw fa-user"></i>关于</a>

  </li>
        <li class="menu-item menu-item-留言">

    <a href="/blog/comments/" rel="section"><i class="fa fa-fw fa-commenting"></i>留言</a>

  </li>
        <li class="menu-item menu-item-commonweal">

    <a href="/blog/404/" rel="section"><i class="fa fa-fw fa-heartbeat"></i>公益 404</a>

  </li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>

</nav>
  <div class="site-search">
    <div class="popup search-popup">
    <div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocorrect="off" autocapitalize="off"
           placeholder="搜索..." spellcheck="false"
           type="search" class="search-input">
  </div>
  <span class="popup-btn-close">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div id="search-result"></div>

</div>
<div class="search-pop-overlay"></div>

  </div>
</div>
    </header>

    
  <div class="back-to-top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>
  <div class="reading-progress-bar"></div>

  <a href="https://github.com/GZC986707252" class="github-corner" title="Follow me on GitHub" aria-label="Follow me on GitHub" rel="noopener" target="_blank"><svg width="80" height="80" viewBox="0 0 250 250" aria-hidden="true"><path d="M0,0 L115,115 L130,115 L142,142 L250,250 L250,0 Z"></path><path d="M128.3,109.0 C113.8,99.7 119.0,89.6 119.0,89.6 C122.0,82.7 120.5,78.6 120.5,78.6 C119.2,72.0 123.4,76.3 123.4,76.3 C127.3,80.9 125.5,87.3 125.5,87.3 C122.9,97.6 130.6,101.9 134.4,103.2" fill="currentColor" style="transform-origin: 130px 106px;" class="octo-arm"></path><path d="M115.0,115.0 C114.9,115.1 118.7,116.5 119.8,115.4 L133.7,101.6 C136.9,99.2 139.9,98.4 142.2,98.6 C133.8,88.0 127.5,74.4 143.8,58.0 C148.5,53.4 154.0,51.2 159.7,51.0 C160.3,49.4 163.2,43.6 171.4,40.1 C171.4,40.1 176.1,42.5 178.8,56.2 C183.1,58.6 187.2,61.8 190.9,65.4 C194.5,69.0 197.7,73.2 200.1,77.6 C213.8,80.2 216.3,84.9 216.3,84.9 C212.7,93.1 206.9,96.0 205.4,96.6 C205.1,102.4 203.0,107.8 198.3,112.5 C181.9,128.9 168.3,122.5 157.7,114.1 C157.9,116.9 156.7,120.9 152.7,124.9 L141.0,136.5 C139.8,137.7 141.6,141.9 141.8,141.8 Z" fill="currentColor" class="octo-body"></path></svg></a>


    <main class="main">
      <div class="main-inner">
        <div class="content-wrap">
          

          <div class="content">
            

  <div class="posts-expand">
      
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block " lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="https://zchao666.gitee.io/blog/2020/03/23/SpringMVC%E7%9A%84%E6%95%B0%E6%8D%AE%E9%AA%8C%E8%AF%81/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/blog/uploads/avatar.jpg">
      <meta itemprop="name" content="小郭">
      <meta itemprop="description" content="一个还在不断学习的编程小白！">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="小郭的博客">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          SpringMVC的数据验证
        </h1>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>
              

              <time title="创建时间：2020-03-23 01:58:44 / 修改时间：01:51:27" itemprop="dateCreated datePublished" datetime="2020-03-23T01:58:44+08:00">2020-03-23</time>
            </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              <span class="post-meta-item-text">分类于</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/blog/categories/SpringMVC%E5%AD%A6%E4%B9%A0/" itemprop="url" rel="index"><span itemprop="name">SpringMVC学习</span></a>
                </span>
            </span>

          
            <span class="post-meta-item" title="阅读次数" id="busuanzi_container_page_pv" style="display: none;">
              <span class="post-meta-item-icon">
                <i class="fa fa-eye"></i>
              </span>
              <span class="post-meta-item-text">阅读次数：</span>
              <span id="busuanzi_value_page_pv"></span>
            </span>
  
  <span class="post-meta-item">
    
      <span class="post-meta-item-icon">
        <i class="fa fa-comment-o"></i>
      </span>
      <span class="post-meta-item-text">Valine：</span>
    
    <a title="valine" href="/blog/2020/03/23/SpringMVC%E7%9A%84%E6%95%B0%E6%8D%AE%E9%AA%8C%E8%AF%81/#valine-comments" itemprop="discussionUrl">
      <span class="post-comments-count valine-comment-count" data-xid="/blog/2020/03/23/SpringMVC%E7%9A%84%E6%95%B0%E6%8D%AE%E9%AA%8C%E8%AF%81/" itemprop="commentCount"></span>
    </a>
  </span>
  
  

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
        <h2 id="数据验证"><a href="#数据验证" class="headerlink" title="数据验证"></a>数据验证</h2><p>数据验证主要用于过滤用户输入的错误数据，保证数据的合法性。<br>数据验证分为<strong>客户端验证</strong> 和 <strong>服务器端验证</strong>。客户端验证主要通过JavaScript代码实现数据合法性校验；服务器端是整个应用程序阻止非法数据的最后防线。<br>在SpringMVC中，进行数据验证可以利用Spring自带的验证器框架验证数据，也可以使用JSR303实现数据验证。</p>
<a id="more"></a>
<h3 id="Spring-验证器"><a href="#Spring-验证器" class="headerlink" title="Spring 验证器"></a>Spring 验证器</h3><h4 id="Validator接口"><a href="#Validator接口" class="headerlink" title="Validator接口"></a>Validator接口</h4><p>创建自定义Spring验证器需要实现<code>org.springframework.validation.Validator</code>接口，该接口主要有两个方法：</p>
<ul>
<li><strong><code>boolean supports(Class&lt;?&gt; aClass)</code></strong>：当返回<code>true</code>时，验证器可以处理指定的<code>Class</code>。</li>
<li><strong><code>void validate(Object object, Errors errors)</code></strong>：该方法功能是验证目标对象<code>object</code>，并将验证的错误消息存放在<code>Errors</code>对象中。</li>
</ul>
<p>往<code>Errors</code>对象存入错误消息主要有<code>reject</code>和<code>rejectValue</code>方法：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">reject</span><span class="params">(String errorCode)</span></span>;</span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">reject</span><span class="params">(String errorCode,String defaultMessage)</span></span>;</span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">rejectValue</span><span class="params">(String field,String errorCode)</span></span>;</span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">rejectValue</span><span class="params">(String field,String errorCode,String defaultMessage)</span></span></span><br></pre></td></tr></table></figure>
<p>一般只要给reject或者rejectValue方法一个错误代码，SpringMVC就会在消息属性文件中查找错误代码，获取错误消息。例如：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">if</span> (productDate != <span class="keyword">null</span> &amp;&amp; productDate.after(<span class="keyword">new</span> Date())) &#123;</span><br><span class="line">	<span class="comment">//date指定验证错误对象的属性域，date.invalid为错误代码</span></span><br><span class="line">	errors.rejectValue(<span class="string">"date"</span>, <span class="string">"date.invalid"</span>);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>在上述中，“<code>date.invalid</code>”就是对应消息属性文件的<code>key</code>。例如有个消息属性文件如下：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">price.invalid=价格在<span class="number">0</span>和<span class="number">100</span>之间</span><br><span class="line">date.invalid=需要的是一个过去的时间</span><br></pre></td></tr></table></figure>
<p>当然也可以直接写入错误消息，即：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">errors.rejectValue(<span class="string">"date"</span>, <span class="string">"需要的是一个过去的时间"</span>);</span><br></pre></td></tr></table></figure>
<h4 id="ValidationUtils类"><a href="#ValidationUtils类" class="headerlink" title="ValidationUtils类"></a>ValidationUtils类</h4><p><code>org.springframework.validation.ValidationUtils</code>是一个工具类，提供了相关的方法帮助判断数据是否为空。<br>例如：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//判断目标对象name属性是否为null或者empty</span></span><br><span class="line">ValidationUtils.rejectIfEmpty(errors, <span class="string">"name"</span>, <span class="string">"商品名称不能为空"</span>);</span><br><span class="line"><span class="comment">//判断目标对象name属性被trim后是否为null或者empty</span></span><br><span class="line">ValidationUtils.rejectIfEmptyOrWhitespace(errors, <span class="string">"name"</span>, <span class="string">"商品名称不能为空"</span>);</span><br></pre></td></tr></table></figure>
<h4 id="验证示例"><a href="#验证示例" class="headerlink" title="验证示例"></a>验证示例</h4><p>需要验证的目标对象</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Product</span> </span>&#123;</span><br><span class="line">	<span class="keyword">private</span> String name; <span class="comment">//不能为空</span></span><br><span class="line">	<span class="keyword">private</span> String description; <span class="comment">//不能为空</span></span><br><span class="line">	<span class="keyword">private</span> <span class="keyword">double</span> price; <span class="comment">//0到100之间</span></span><br><span class="line">	<span class="meta">@DateTimeFormat</span>(pattern = <span class="string">"yyyy-MM-dd"</span>)</span><br><span class="line">	<span class="keyword">private</span> Date date;  <span class="comment">//不能在当前时间之后</span></span><br><span class="line">	<span class="comment">//省略setter和getter方法</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>使用Spring验证器进行数据验证，不需要添加额外配置，只需要添加<code>@Component</code>注解将自定义验证器类声明为验证组件，并在Controller控制器中使用<code>@Resource</code>或者<code>@Autowired</code>注解注入自定义验证器。<br>自定义验证器类</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Component</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">ProductValidator</span> <span class="keyword">implements</span> <span class="title">Validator</span> </span>&#123;</span><br><span class="line">	<span class="meta">@Override</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">boolean</span> <span class="title">supports</span><span class="params">(Class&lt;?&gt; aClass)</span> </span>&#123;</span><br><span class="line">		<span class="comment">// 要验证的model,返回值为false则不验证</span></span><br><span class="line">		<span class="comment">// 判断参数aClass是否是Product本身或者子类</span></span><br><span class="line">		<span class="keyword">return</span> Product<span class="class">.<span class="keyword">class</span>.<span class="title">isAssignableFrom</span>(<span class="title">aClass</span>)</span>;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="meta">@Override</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">validate</span><span class="params">(Object object, Errors errors)</span> </span>&#123;</span><br><span class="line">		Product product = (Product) object; <span class="comment">// object要验证的对象</span></span><br><span class="line">		<span class="comment">// 往Errors对象存入错误消息</span></span><br><span class="line">		ValidationUtils.rejectIfEmpty(errors, <span class="string">"name"</span>, <span class="string">"商品名称不能为空"</span>);</span><br><span class="line">		ValidationUtils.rejectIfEmpty(errors, <span class="string">"description"</span>, <span class="string">"商品描述不能为空"</span>);</span><br><span class="line">		<span class="keyword">if</span> (product.getPrice() &gt; <span class="number">100</span> || product.getPrice() &lt; <span class="number">0</span>) &#123;</span><br><span class="line">			errors.rejectValue(<span class="string">"price"</span>, <span class="string">"价格在0和100之间"</span>);</span><br><span class="line">		&#125;</span><br><span class="line">		Date productDate = product.getDate();</span><br><span class="line">		<span class="comment">// 在系统时间之后</span></span><br><span class="line">		<span class="keyword">if</span> (productDate != <span class="keyword">null</span> &amp;&amp; productDate.after(<span class="keyword">new</span> Date())) &#123;</span><br><span class="line">			errors.rejectValue(<span class="string">"date"</span>, <span class="string">"创建日期不能是将来时间"</span>);</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>在控制器中执行验证</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Controller</span></span><br><span class="line"><span class="meta">@RequestMapping</span>(<span class="string">"/product"</span>)</span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">ProductController</span> </span>&#123;</span><br><span class="line">	<span class="comment">//省略相关代码</span></span><br><span class="line">	<span class="meta">@Resource</span></span><br><span class="line">	<span class="keyword">private</span> Validator validator;</span><br><span class="line">	</span><br><span class="line">	<span class="comment">//省略相关代码</span></span><br><span class="line">	<span class="meta">@RequestMapping</span>(<span class="string">"/add"</span>)</span><br><span class="line">	<span class="comment">// BindingResult对象用于接收校验返回的错误消息，必须紧跟在验证对象的后面(两者相邻)</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> String <span class="title">addProduct</span><span class="params">(Product product, BindingResult result, Model model)</span> </span>&#123;</span><br><span class="line">	<span class="comment">//执行数据验证，传入目标对象和错误消息接收对象</span></span><br><span class="line">		<span class="keyword">this</span>.validator.validate(product, result);</span><br><span class="line">		<span class="keyword">if</span> (result.hasErrors()) &#123; <span class="comment">// 如果校验错误</span></span><br><span class="line">			<span class="keyword">for</span> (FieldError error : result.getFieldErrors()) &#123;</span><br><span class="line">			<span class="comment">//后台打印错误消息</span></span><br><span class="line">				System.out.println(error.getField() + <span class="string">":"</span> + error.getDefaultMessage());</span><br><span class="line">			&#125;</span><br><span class="line">			<span class="comment">//把错误消息写进model,用于前端显示</span></span><br><span class="line">			model.addAttribute(<span class="string">"errors"</span>, result.getFieldErrors());</span><br><span class="line">			<span class="keyword">return</span> <span class="string">"addProduct"</span>;</span><br><span class="line">		&#125;</span><br><span class="line">		<span class="comment">//省略相关代码</span></span><br><span class="line">		<span class="keyword">return</span> <span class="string">"productList"</span>;</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="JSR-303验证"><a href="#JSR-303验证" class="headerlink" title="JSR 303验证"></a>JSR 303验证</h3><p>JSR 303验证主要有两个方式实现，一个是Hibernate Validator，一个是Apache BVal 。这里主要使用<strong>Hibernate Validator</strong>进行数据验证。</p>
<h4 id="下载"><a href="#下载" class="headerlink" title="下载"></a>下载</h4><ul>
<li><a href="https://pan.baidu.com/s/1I2fbOq-u3SQ5ZXJ3RnxwoA" target="_blank" rel="noopener">整合了所需的jar</a> 【提取码：gg7y】</li>
</ul>
<h4 id="配置验证器"><a href="#配置验证器" class="headerlink" title="配置验证器"></a>配置验证器</h4><figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">&lt;!-- 配置数据校验 --&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">bean</span> <span class="attr">id</span>=<span class="string">"localValidator"</span> <span class="attr">class</span>=<span class="string">"org.springframework.validation.beanvalidation.LocalValidatorFactoryBean"</span>&gt;</span></span><br><span class="line">	<span class="comment">&lt;!-- </span></span><br><span class="line"><span class="comment">		如果将错误消息放在属性文件,则需要配置属性文件，</span></span><br><span class="line"><span class="comment">		并且配置以下属性，将属性文件与HibernateValidator关联</span></span><br><span class="line"><span class="comment">	--&gt;</span></span><br><span class="line">	<span class="comment">&lt;!-- Hibernate校验器 --&gt;</span></span><br><span class="line">	<span class="comment">&lt;!-- &lt;property name="providerClass" value="org.hibernate.validator.HibernateValidator"/&gt; --&gt;</span></span><br><span class="line">	<span class="comment">&lt;!-- 指定校验使用的资源文件，在文件中配置校验错误消息，如果不指定，默认使用classpath下的ValidationMessages.properties --&gt;</span></span><br><span class="line">	<span class="comment">&lt;!-- &lt;property name="validationMessageSource" ref=""/&gt; --&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">bean</span>&gt;</span></span><br><span class="line"><span class="comment">&lt;!-- 配置属性文件关联HibernateValidator时需要设置validator的值--&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">mvc:annotation-driven</span> <span class="attr">validator</span>=<span class="string">"localValidator"</span>/&gt;</span></span><br></pre></td></tr></table></figure>
<p>如果没有属性文件，则直接配置<code>LocalValidatorFactoryBean</code>即可。</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">bean</span> <span class="attr">id</span>=<span class="string">"localValidator"</span> <span class="attr">class</span>=<span class="string">"org.springframework.validation.beanvalidation.LocalValidatorFactoryBean"</span>/&gt;</span></span><br></pre></td></tr></table></figure>
<h4 id="标注类型"><a href="#标注类型" class="headerlink" title="标注类型"></a>标注类型</h4><p>JSR 303不需要写验证器，但需要使用它的标注类型在模型属性上进行约束。</p>
<h5 id="空检查"><a href="#空检查" class="headerlink" title="空检查"></a>空检查</h5><ul>
<li><code>@Null</code> ：验证对象是否为null，不为null即验证错误。</li>
<li><code>@NotNull</code> ：验证对象是否为null，无法检测长度为0的字符串。</li>
<li><code>@NotBlank</code>：检查约束字符串是不是null，并且判断被trim后长度是否大于0。</li>
<li><code>@NotEmpty</code> ：检查约束元素是否为null，或者为empty。</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@NotBlank</span>(message = <span class="string">"商品名不能为空"</span>)</span><br><span class="line"><span class="keyword">private</span> String name;</span><br></pre></td></tr></table></figure>
<h5 id="boolean检查"><a href="#boolean检查" class="headerlink" title="boolean检查"></a>boolean检查</h5><ul>
<li><code>@AssertTrue</code>：验证boolean属性是否为true。</li>
<li><code>@AssertFalse</code>：验证boolean属性是否为true。<h5 id="长度检查"><a href="#长度检查" class="headerlink" title="长度检查"></a>长度检查</h5></li>
<li><code>@Size(min= ,max= )</code>：验证对象（Array、Collection、Map、String）长度是否在给定范围。</li>
<li><code>@Length(min= ,max= )</code>：验证字符串长度是否在给定范围。<h5 id="日期检查"><a href="#日期检查" class="headerlink" title="日期检查"></a>日期检查</h5></li>
<li><code>@Past</code>：验证Date和Calendar对象是否在当前时间之前。</li>
<li><code>@Future</code>：验证Date和Calendar对象是否在当前时间之后。<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Past</span>(message = <span class="string">"创建日期需要是一个过去时间"</span>)</span><br><span class="line"><span class="keyword">private</span> Date date;</span><br></pre></td></tr></table></figure>
<h5 id="数值检查"><a href="#数值检查" class="headerlink" title="数值检查"></a>数值检查</h5></li>
<li><code>@Min</code>：验证Number和String对象是否大于指定值。</li>
<li><code>@Max</code>：验证Number和String对象是否小于指定值。</li>
<li><code>@DecimalMax</code>： 被标注的值必须不大于约束中指定的最大值，这个约束的參数是一个通过BigDecimal定义的最大值的字符串表示，小数存在精度。</li>
<li><code>@DecimalMin</code>：被标注的值必须不小于约束中指定的最小值，这个约束的参数是一个通过BigDecimal定义的最小值的字符串表示，小数存在精度。</li>
<li><code>@Digits</code>： 验证Number和String的构成是否合法。</li>
<li><code>@Digits(integer= , fraction=)</code>: 验证字符串是否符合指定格式的数字，integer 指定整数精度，fraction 指定小数精度。</li>
<li><code>@Range(min= , max= )</code>：检查数字是否介于min和max之间。</li>
<li><code>@CreditCardNumber</code>：信用卡验证。</li>
<li><code>@Email</code>：验证是否为邮件地址，如果为null，不进行验证， 直接通过。<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Range</span>(min = <span class="number">0</span>, max = <span class="number">100</span>, message = <span class="string">"价格只能在0和100之间"</span>)</span><br><span class="line"><span class="keyword">private</span> <span class="keyword">double</span> price;</span><br></pre></td></tr></table></figure>

</li>
</ul>
<h5 id="其他检查"><a href="#其他检查" class="headerlink" title="其他检查"></a>其他检查</h5><ul>
<li><code>@Pattern</code>：验证String对象是否符合正则表达式。</li>
<li><code>@Valid</code>： 对关联对象进行校验，如果关联对象是个集合或者数组，那么对其中的元素进行校验，如果是一个map， 则对其中的值部分进行校验。</li>
</ul>
<p>在Controller控制器中的处理方法中，通过<code>@Valid</code>注解指定需要验证的对象。</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@RequestMapping</span>(<span class="string">"/add"</span>)</span><br><span class="line"><span class="comment">// BindingResult对象用于接收校验返回的错误消息，必须紧跟在验证对象的后面(两者相邻)</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> String <span class="title">addProduct</span><span class="params">(@Valid Product product, BindingResult result, Model model)</span></span></span><br></pre></td></tr></table></figure>
<h4 id="验证示例-1"><a href="#验证示例-1" class="headerlink" title="验证示例"></a>验证示例</h4><p>需要验证的目标对象类</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Product</span> </span>&#123;</span><br><span class="line">	<span class="meta">@NotBlank</span>(message = <span class="string">"商品名不能为空"</span>)</span><br><span class="line">	<span class="keyword">private</span> String name;</span><br><span class="line"></span><br><span class="line">	<span class="meta">@NotBlank</span>(message = <span class="string">"商品描述不能为空"</span>)</span><br><span class="line">	<span class="keyword">private</span> String description;</span><br><span class="line"></span><br><span class="line">	<span class="meta">@Range</span>(min = <span class="number">0</span>, max = <span class="number">100</span>, message = <span class="string">"价格只能在0和100之间"</span>)</span><br><span class="line">	<span class="keyword">private</span> <span class="keyword">double</span> price;</span><br><span class="line"></span><br><span class="line">	<span class="meta">@Past</span>(message = <span class="string">"创建日期需要是一个过去时间"</span>)</span><br><span class="line">	<span class="meta">@DateTimeFormat</span>(pattern = <span class="string">"yyyy-MM-dd"</span>)</span><br><span class="line">	<span class="keyword">private</span> Date date;</span><br><span class="line">	<span class="comment">//省略setter和getter方法</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>控制器关键代码</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Controller</span></span><br><span class="line"><span class="meta">@RequestMapping</span>(<span class="string">"/product"</span>)</span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">ProductController</span> </span>&#123;</span><br><span class="line">	<span class="comment">// 获得日志记录对象</span></span><br><span class="line">	<span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">final</span> Log logger = LogFactory.getLog(ProductController<span class="class">.<span class="keyword">class</span>)</span>;</span><br><span class="line">	<span class="meta">@Autowired</span></span><br><span class="line">	<span class="keyword">private</span> ProductService productService;</span><br><span class="line">	</span><br><span class="line">	<span class="meta">@RequestMapping</span>(<span class="string">"/add"</span>)</span><br><span class="line">	<span class="comment">// BindingResult对象用于接收校验返回的错误消息，必须紧跟在验证对象的后面(两者相邻)</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> String <span class="title">addProduct</span><span class="params">(@Valid Product product, BindingResult result, Model model)</span> </span>&#123;</span><br><span class="line">		<span class="keyword">if</span> (result.hasErrors()) &#123; <span class="comment">// 如果校验错误</span></span><br><span class="line">			<span class="keyword">for</span> (FieldError error : result.getFieldErrors()) &#123;</span><br><span class="line">				System.out.println(error.getField() + <span class="string">":"</span> + error.getDefaultMessage());</span><br><span class="line">			&#125;</span><br><span class="line">			model.addAttribute(<span class="string">"errors"</span>, result.getFieldErrors());</span><br><span class="line">			<span class="keyword">return</span> <span class="string">"addProduct"</span>;</span><br><span class="line">		&#125;</span><br><span class="line">		<span class="keyword">if</span> (!productService.add(product)) &#123;</span><br><span class="line">			<span class="keyword">return</span> <span class="string">"addProduct"</span>;</span><br><span class="line">		&#125;</span><br><span class="line">		logger.info(<span class="string">"添加成功"</span>);</span><br><span class="line">		model.addAttribute(<span class="string">"products"</span>, productService.getProducts());</span><br><span class="line">		<span class="keyword">return</span> <span class="string">"productList"</span>;</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="运行效果"><a href="#运行效果" class="headerlink" title="运行效果"></a>运行效果</h3><p><img src="https://img-blog.csdnimg.cn/20200323014337648.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQxMTkzMTMz,size_16,color_FFFFFF,t_70" alt="在这里插入图片描述"><br><img src="https://img-blog.csdnimg.cn/20200323014447164.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQxMTkzMTMz,size_16,color_FFFFFF,t_70" alt="在这里插入图片描述"></p>

    </div>

    
    
    
        <div class="reward-container">
  <div></div>
  <button onclick="var qr = document.getElementById('qr'); qr.style.display = (qr.style.display === 'none') ? 'block' : 'none';">
    打赏
  </button>
  <div id="qr" style="display: none;">
      
      <div style="display: inline-block;">
        <img src="/blog/uploads/wechatpay.jpg" alt="小郭 微信支付">
        <p>微信支付</p>
      </div>

  </div>
</div>


      <footer class="post-footer">
          
          <div class="post-tags">
              <a href="/blog/tags/Java/" rel="tag"><i class="fa fa-tag"></i> Java</a>
              <a href="/blog/tags/%E6%95%B0%E6%8D%AE%E9%AA%8C%E8%AF%81/" rel="tag"><i class="fa fa-tag"></i> 数据验证</a>
              <a href="/blog/tags/JSR303/" rel="tag"><i class="fa fa-tag"></i> JSR303</a>
          </div>

        


        
    <div class="post-nav">
      <div class="post-nav-item">
    <a href="/blog/2020/03/22/SpringMVC%20%E5%AE%9E%E7%8E%B0%E6%96%87%E4%BB%B6%E7%9A%84%E4%B8%8A%E4%BC%A0%E5%92%8C%E4%B8%8B%E8%BD%BD/" rel="prev" title="SpringMVC实现文件的上传和下载">
      <i class="fa fa-chevron-left"></i> SpringMVC实现文件的上传和下载
    </a></div>
      <div class="post-nav-item">
    <a href="/blog/2020/03/26/SpringMVC%E7%9A%84%E7%BB%9F%E4%B8%80%E5%BC%82%E5%B8%B8%E5%A4%84%E7%90%86/" rel="next" title="SpringMVC的统一异常处理">
      SpringMVC的统一异常处理 <i class="fa fa-chevron-right"></i>
    </a></div>
    </div>
      </footer>
    
  </article>
  
  
  

  </div>


          </div>
          
    <div class="comments" id="valine-comments"></div>

<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      let commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>

        </div>
          
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line toggle-line-first"></span>
    <span class="toggle-line toggle-line-middle"></span>
    <span class="toggle-line toggle-line-last"></span>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
          <div class="post-toc motion-element"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#数据验证"><span class="nav-text">数据验证</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#Spring-验证器"><span class="nav-text">Spring 验证器</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#Validator接口"><span class="nav-text">Validator接口</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#ValidationUtils类"><span class="nav-text">ValidationUtils类</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#验证示例"><span class="nav-text">验证示例</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#JSR-303验证"><span class="nav-text">JSR 303验证</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#下载"><span class="nav-text">下载</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#配置验证器"><span class="nav-text">配置验证器</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#标注类型"><span class="nav-text">标注类型</span></a><ol class="nav-child"><li class="nav-item nav-level-5"><a class="nav-link" href="#空检查"><span class="nav-text">空检查</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#boolean检查"><span class="nav-text">boolean检查</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#长度检查"><span class="nav-text">长度检查</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#日期检查"><span class="nav-text">日期检查</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#数值检查"><span class="nav-text">数值检查</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#其他检查"><span class="nav-text">其他检查</span></a></li></ol></li><li class="nav-item nav-level-4"><a class="nav-link" href="#验证示例-1"><span class="nav-text">验证示例</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#运行效果"><span class="nav-text">运行效果</span></a></li></ol></li></ol></div>
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="小郭"
      src="/blog/uploads/avatar.jpg">
  <p class="site-author-name" itemprop="name">小郭</p>
  <div class="site-description" itemprop="description">一个还在不断学习的编程小白！</div>
</div>
<div class="site-state-wrap motion-element">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/blog/archives/">
        
          <span class="site-state-item-count">26</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
            <a href="/blog/categories/">
          
        <span class="site-state-item-count">7</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
            <a href="/blog/tags/">
          
        <span class="site-state-item-count">44</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>
  <div class="links-of-author motion-element">
      <span class="links-of-author-item">
        <a href="https://github.com/GZC986707252" title="GitHub → https:&#x2F;&#x2F;github.com&#x2F;GZC986707252" rel="noopener" target="_blank"><i class="fa fa-fw fa-github"></i>GitHub</a>
      </span>
      <span class="links-of-author-item">
        <a href="https://weibo.com/u/3278894871" title="微博 → https:&#x2F;&#x2F;weibo.com&#x2F;u&#x2F;3278894871" rel="noopener" target="_blank"><i class="fa fa-fw fa-weibo"></i>微博</a>
      </span>
      <span class="links-of-author-item">
        <a href="https://blog.csdn.net/qq_41193133" title="CSDN → https:&#x2F;&#x2F;blog.csdn.net&#x2F;qq_41193133" rel="noopener" target="_blank"><i class="fa fa-fw fa-globe"></i>CSDN</a>
      </span>
      <span class="links-of-author-item">
        <a href="mailto:986707252@qq.com" title="E-Mail → mailto:986707252@qq.com" rel="noopener" target="_blank"><i class="fa fa-fw fa-envelope"></i>E-Mail</a>
      </span>
  </div>



      </div>

    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer class="footer">
      <div class="footer-inner">
        

<div class="copyright">
  
  &copy; 
  <span itemprop="copyrightYear">2020</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">小郭</span>
</div>
  <div class="powered-by">由 <a href="https://hexo.io/" class="theme-link" rel="noopener" target="_blank">Hexo</a> 强力驱动 v4.2.0
  </div>
  <span class="post-meta-divider">|</span>
  <div class="theme-info">主题 – <a href="https://theme-next.org/" class="theme-link" rel="noopener" target="_blank">NexT.Gemini</a> v7.7.2
  </div>

        
<div class="busuanzi-count">
  <script async src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>
    <span class="post-meta-item" id="busuanzi_container_site_uv" style="display: none;">
      <span class="post-meta-item-icon">
        <i class="fa fa-user"></i>
      </span>
      <span class="site-uv" title="总访客量">
        <span id="busuanzi_value_site_uv"></span>
      </span>
    </span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item" id="busuanzi_container_site_pv" style="display: none;">
      <span class="post-meta-item-icon">
        <i class="fa fa-eye"></i>
      </span>
      <span class="site-pv" title="总访问量">
        <span id="busuanzi_value_site_pv"></span>
      </span>
    </span>
</div>








      </div>
    </footer>
  </div>

  
  <script src="/blog/lib/anime.min.js"></script>
  <script src="/blog/lib/velocity/velocity.min.js"></script>
  <script src="/blog/lib/velocity/velocity.ui.min.js"></script>

<script src="/blog/js/utils.js"></script>

<script src="/blog/js/motion.js"></script>


<script src="/blog/js/schemes/pisces.js"></script>


<script src="/blog/js/next-boot.js"></script>


  <script defer src="/blog/lib/three/three.min.js"></script>
    <script defer src="/blog/lib/three/canvas_lines.min.js"></script>


  
  <script>
    (function(){
      var bp = document.createElement('script');
      var curProtocol = window.location.protocol.split(':')[0];
      bp.src = (curProtocol === 'https') ? 'https://zz.bdstatic.com/linksubmit/push.js' : 'http://push.zhanzhang.baidu.com/push.js';
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(bp, s);
    })();
  </script>




  
<script src="/blog/js/local-search.js"></script>













  

  


<script>
NexT.utils.loadComments(document.querySelector('#valine-comments'), () => {
  NexT.utils.getScript('//unpkg.com/valine/dist/Valine.min.js', () => {
    var GUEST = ['nick', 'mail', 'link'];
    var guest = '昵称,邮箱,链接';
    guest = guest.split(',').filter(item => {
      return GUEST.includes(item);
    });
    new Valine({
      el         : '#valine-comments',
      verify     : false,
      notify     : true,
      appId      : 'dQIv9pCVNauk7ezmEU35I8sW-gzGzoHsz',
      appKey     : '2BGfzddNSeijXJJtTofkbuJx',
      placeholder: "走过路过，千万不要错过，一起聊聊吧！",
      avatar     : 'mm',
      meta       : guest,
      pageSize   : '10' || 10,
      visitor    : false,
      lang       : '' || 'zh-cn',
      path       : location.pathname,
      recordIP   : false,
      serverURLs : ''
    });
  }, window.Valine);
});
</script>

</body>
</html>